<template>
  <div class="glass-system-page">
    <!-- Page Header -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-title">
          <div class="title-icon">
            <svg-icon icon-class="date" />
          </div>
          <div class="title-content">
            <h1>请假信息管理</h1>
            <p>Leave Management - 员工请假申请管理</p>
          </div>
        </div>
        <div class="header-actions">
          <button class="glass-button glass-button--primary" @click="handleAdd" v-hasPermi="['leave:leave:add']">
            <svg-icon icon-class="plus" />
            <span>新增请假</span>
          </button>
        </div>
      </div>
    </div>

    <!-- Search Form -->
    <div class="search-form" v-show="showSearch">
      <div class="glass-form">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
          <el-form-item label="请假时间" prop="leaveTime">
            <el-date-picker 
              clearable
              v-model="queryParams.leaveTime"
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择请假时间"
              style="width: 200px">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="销假时间" prop="backTime">
            <el-date-picker 
              clearable
              v-model="queryParams.backTime"
              type="datetime"
              value-format="YYYY-MM-DD HH:mm:ss"
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择销假时间"
              style="width: 200px">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="queryParams.type" placeholder="请选择请假类型" clearable style="width: 200px">
              <el-option v-for="dict in leave_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择申请表状态" clearable style="width: 200px">
              <el-option v-for="dict in leave_status" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <button type="button" class="glass-button glass-button--primary" @click="handleQuery">
              <svg-icon icon-class="search" />
              <span>搜索</span>
            </button>
            <button type="button" class="glass-button" @click="resetQuery" style="margin-left: 8px;">
              <svg-icon icon-class="refresh" />
              <span>重置</span>
            </button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- Action Bar -->
    <div class="action-bar">
      <div class="action-content">
        <div class="action-buttons">
          <button class="glass-button glass-button--danger" :disabled="multiple" @click="handleDelete" v-hasPermi="['leave:leave:remove']">
            <svg-icon icon-class="delete" />
            <span>删除</span>
          </button>
          <button class="glass-button" @click="handleExport" v-hasPermi="['leave:leave:export']">
            <svg-icon icon-class="download" />
            <span>导出</span>
          </button>
        </div>
        <div class="action-tools">
          <div class="tool-item" @click="showSearch = !showSearch">
            <svg-icon icon-class="search" />
            <span>显示搜索</span>
          </div>
          <div class="tool-item" @click="getList">
            <svg-icon icon-class="refresh" />
            <span>刷新</span>
          </div>
        </div>
      </div>
    </div>

    <!-- Data Table -->
    <div class="data-table">
      <div class="table-container">
        <el-table v-loading="loading" :data="leaveList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="id" width="80">
            <template #default="scope">
              <span class="leave-id">#{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column label="申请人" align="center" prop="userName" min-width="120">
            <template #default="scope">
              <div class="user-info centered-content">
                <svg-icon icon-class="user" class="user-icon" />
                <span class="user-name">{{ getUserNickName(scope.row.cLeaveUser?.userId) }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="请假时间" align="center" prop="leaveTime" width="160">
            <template #default="scope">
              <div class="time-info">
                <svg-icon icon-class="date" class="time-icon" />
                <span>{{ parseTime(scope.row.leaveTime, '{y}-{m}-{d} {h}:{i}') }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="销假时间" align="center" prop="backTime" width="160">
            <template #default="scope">
              <div class="time-info">
                <svg-icon icon-class="date" class="time-icon" />
                <span>{{ parseTime(scope.row.backTime, '{y}-{m}-{d} {h}:{i}') }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="类型" align="center" prop="type" width="100">
            <template #default="scope">
              <span :class="['leave-type-tag', getLeaveTypeClass(scope.row.type)]">
                {{ getLeaveTypeText(scope.row.type) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status" width="100">
            <template #default="scope">
              <span :class="['glass-status-tag', getStatusClass(scope.row.status)]">
                {{ getStatusText(scope.row.status) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="请假原因" prop="reason" min-width="150" show-overflow-tooltip>
            <template #default="scope">
              <span class="reason-text">{{ scope.row.reason || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="260" class-name="small-padding fixed-width">
            <template #default="scope">
              <div class="table-actions">
                <el-tooltip content="查看详情" placement="top">
                  <button class="action-btn action-btn--primary action-btn--wide" @click="handleViewDetail(scope.row)">
                    <svg-icon icon-class="view" />
                    <span>查看详情</span>
                  </button>
                </el-tooltip>
                <!-- 只对已通过的请假申请显示销假按钮 -->
                <el-tooltip content="设置已销假" placement="top" v-if="scope.row.status == 0 || scope.row.status === '0'">
                  <button class="action-btn action-btn--success" @click="handleSetCompleted(scope.row)" v-hasPermi="['leave:leave:edit']">
                    <svg-icon icon-class="check" />
                  </button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top">
                  <button class="action-btn action-btn--danger" @click="handleDelete(scope.row)" v-hasPermi="['leave:leave:remove']">
                    <svg-icon icon-class="delete" />
                  </button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    
    <!-- Pagination -->
    <div class="pagination-container">
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改请假信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="leaveRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="请假时间" prop="leaveTime">
          <el-date-picker clearable
            v-model="form.leaveTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择请假时间"
            style="width: 100%"
            :disabled="form.id != null">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="销假时间" prop="backTime">
          <el-date-picker clearable
            v-model="form.backTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择销假时间"
            style="width: 100%"
            :disabled="form.id != null">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请假原因" prop="reason">
          <el-input
            v-model="form.reason"
            type="textarea"
            :rows="3"
            placeholder="请输入请假原因"
            maxlength="500"
            show-word-limit
            :disabled="form.id != null"
          />
        </el-form-item>
        <el-form-item label="请假类型" prop="type">
          <el-select 
            v-if="form.id == null"
            v-model="form.type"
            placeholder="请选择请假类型"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in leave_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <div v-else class="leave-type-display">
            <span :class="['leave-type-tag', getLeaveTypeClass(form.type)]">
              {{ getLeaveTypeText(form.type) }}
            </span>
          </div>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="员工名称" prop="cLeaveUser.userId">
              <el-select 
                v-model="form.cLeaveUser.userId" 
                placeholder="请选择员工" 
                clearable 
                style="width: 100%"
                :disabled="form.id != null"
                @change="handleUserChange"
              >
                <el-option
                  v-for="user in userOptions"
                  :key="user.userId"
                  :label="user.nickName"
                  :value="user.userId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户名称">
              <el-input 
                v-model="selectedUserName" 
                disabled 
                style="width: 100%"
                placeholder="选择员工后自动显示"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div v-if="form.id != null && isStatusProcessed" class="glass-dialog-footer-single">
          <div class="glass-status-display">
            <span :class="['glass-status-badge', getStatusButtonClass()]">
              {{ getStatusButtonText() }}
            </span>
          </div>
        </div>
        <div v-else-if="form.id != null && !isStatusProcessed" class="glass-dialog-footer">
          <button class="glass-footer-btn glass-footer-btn--success" @click="handleApprove">
            <svg-icon icon-class="check" />
            <span>通过</span>
          </button>
          <button class="glass-footer-btn glass-footer-btn--danger" @click="handleReject">
            <svg-icon icon-class="close" />
            <span>拒绝</span>
          </button>
        </div>
        <div v-else class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Leave">
import { listLeave, getLeave, delLeave, addLeave, updateLeave } from "@/api/leave/leave"
import { listUser } from "@/api/system/user"

const { proxy } = getCurrentInstance()
const { leave_status, leave_type } = proxy.useDict("leave_status", "leave_type")

const leaveList = ref([])
const userOptions = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const selectedUserName = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    leaveTime: null,
    backTime: null,
    type: null,
    status: null
  },
  rules: {
    leaveTime: [
      { required: true, message: "请假时间不能为空", trigger: "blur" }
    ],
    backTime: [
      { required: true, message: "销假时间不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "请假类型不能为空", trigger: "change" }
    ],
    reason: [
      { required: true, message: "请假原因不能为空", trigger: "blur" }
    ],
    "cLeaveUser.userId": [
      { required: true, message: "员工名称不能为空", trigger: "change" }
    ],
    status: []
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 判断状态是否为已处理（已通过或已拒绝） */
const isStatusProcessed = computed(() => {
  if (!form.value || form.value.status === null || form.value.status === undefined || form.value.status === '') {
    return false
  }
  
  if (!leave_status.value || leave_status.value.length === 0) {
    return false
  }
  
  // 将状态值转换为字符串进行比较
  const statusValue = String(form.value.status)
  
  // 查找匹配的状态字典项
  const currentStatus = leave_status.value.find(dict => {
    const dictValue = String(dict.value)
    return dictValue === statusValue
  })
  
  if (!currentStatus) {
    // 如果找不到匹配的字典项，直接根据状态值判断（常见值：1=通过，2=拒绝）
    if (statusValue === "1" || statusValue === "2") {
      return true
    }
    return false
  }
  
  const statusLabel = String(currentStatus.label || "")
  
  // 判断是否为已通过或已拒绝状态
  const isApprovedStatus = statusLabel === "通过" || 
                           statusLabel === "已通过" || 
                           statusLabel === "批准" ||
                           statusLabel === "已批准" ||
                           (statusLabel.includes("通过") && !statusLabel.includes("待"))
  
  const isRejectedStatus = statusLabel === "拒绝" || 
                           statusLabel === "已拒绝" || 
                           statusLabel === "驳回" ||
                           statusLabel === "已驳回" ||
                           (statusLabel.includes("拒绝") && !statusLabel.includes("待"))
  
  return isApprovedStatus || isRejectedStatus
})

/** 判断是否为已通过状态 */
const isApproved = computed(() => {
  if (!form.value || form.value.status === null || form.value.status === undefined || form.value.status === '') {
    return false
  }
  
  if (!leave_status.value || leave_status.value.length === 0) {
    return false
  }
  
  // 将状态值转换为字符串进行比较
  const statusValue = String(form.value.status)
  
  // 查找匹配的状态字典项
  const currentStatus = leave_status.value.find(dict => {
    const dictValue = String(dict.value)
    return dictValue === statusValue
  })
  
  if (!currentStatus) {
    // 如果找不到匹配的字典项，直接根据状态值判断（1=通过）
    return statusValue === "1"
  }
  
  const statusLabel = String(currentStatus.label || "")
  
  // 判断是否为已通过状态
  return statusLabel === "通过" || 
         statusLabel === "已通过" || 
         statusLabel === "批准" ||
         statusLabel === "已批准" ||
         (statusLabel.includes("通过") && !statusLabel.includes("待"))
})

/** 获取状态按钮文字 */
function getStatusButtonText() {
  if (!form.value || form.value.status === null || form.value.status === undefined) {
    return ""
  }
  
  if (isApproved.value) {
    return "已通过"
  } else if (isStatusProcessed.value) {
    return "已拒绝"
  }
  return ""
}

/** 获取状态按钮样式类 */
function getStatusButtonClass() {
  if (isApproved.value) {
    return "status-approved"
  } else if (isStatusProcessed.value) {
    return "status-rejected"
  }
  return ""
}

/** 查询请假信息管理列表 */
function getList() {
  loading.value = true
  listLeave(queryParams.value).then(response => {
    // 对数据进行排序：待审核(0) > 已通过(1) > 已拒绝(2)
    const sortedRows = response.rows.sort((a, b) => {
      // 确保状态值为数字类型进行比较
      const statusA = parseInt(a.status) || 0
      const statusB = parseInt(b.status) || 0
      
      // 状态优先级：0(待审核) < 1(已通过) < 2(已拒绝)
      // 待审核优先级最高，排在最前面
      const statusPriority = { 0: 0, 1: 1, 2: 2 }
      const priorityA = statusPriority[statusA] !== undefined ? statusPriority[statusA] : 999
      const priorityB = statusPriority[statusB] !== undefined ? statusPriority[statusB] : 999
      
      console.log(`排序比较: A(status=${statusA}, priority=${priorityA}) vs B(status=${statusB}, priority=${priorityB})`)
      
      if (priorityA !== priorityB) {
        return priorityA - priorityB
      }
      
      // 如果状态相同，按创建时间倒序排列（最新的在前）
      const timeA = new Date(a.createTime || 0)
      const timeB = new Date(b.createTime || 0)
      return timeB - timeA
    })
    
    leaveList.value = sortedRows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    leaveTime: null,
    backTime: null,
    type: null,
    status: null,
    reason: null,
    cLeaveUser: {
      userId: null
    }
  }
  selectedUserName.value = ""
  proxy.resetForm("leaveRef")
  // 重置后重新设置验证规则
  updateRules()
}

// 处理员工选择变化
function handleUserChange(userId) {
  if (userId) {
    const user = userOptions.value.find(u => u.userId == userId)
    selectedUserName.value = user ? user.userName : ""
  } else {
    selectedUserName.value = ""
  }
}

/** 更新验证规则 */
function updateRules() {
  // 根据是新增还是修改来动态设置验证规则
  if (form.value.id != null) {
    // 修改时，需要验证状态
    rules.value.status = [
      { required: true, message: "申请表状态不能为空", trigger: "change" }
    ]
  } else {
    // 新增时，不需要验证状态
    rules.value.status = []
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  // 新增时设置默认状态（如果需要）
  form.value.status = null
  open.value = true
  title.value = "添加请假信息管理"
  updateRules()
}

/** 查看详情按钮操作 */
function handleViewDetail(row) {
  reset()
  const _id = row.id || ids.value
  getLeave(_id).then(response => {
    form.value = response.data
    // 确保状态值存在
    if (form.value.status === undefined || form.value.status === null) {
      form.value.status = ''
    }
    // 确保 cLeaveUser 对象存在
    if (!form.value.cLeaveUser) {
      form.value.cLeaveUser = { userId: null }
    }
    // 设置账户名称
    if (form.value.cLeaveUser && form.value.cLeaveUser.userId) {
      handleUserChange(form.value.cLeaveUser.userId)
    }
    open.value = true
    title.value = "请假信息详情"
    updateRules()
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getLeave(_id).then(response => {
    form.value = response.data
    // 确保 cLeaveUser 对象存在
    if (!form.value.cLeaveUser) {
      form.value.cLeaveUser = { userId: null }
    }
    // 设置账户名称
    if (form.value.cLeaveUser && form.value.cLeaveUser.userId) {
      handleUserChange(form.value.cLeaveUser.userId)
    }
    open.value = true
    title.value = "修改请假信息管理"
    updateRules()
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["leaveRef"].validate(valid => {
    if (valid) {
      // 验证请假时间不能晚于销假时间
      if (form.value.leaveTime && form.value.backTime) {
        const leaveTime = new Date(form.value.leaveTime)
        const backTime = new Date(form.value.backTime)
        if (leaveTime >= backTime) {
          proxy.$modal.msgError("请假时长必须大于0")
          return
        }
      }
      
      // 处理 cLeaveUser 对象
      if (!form.value.cLeaveUser || !form.value.cLeaveUser.userId) {
        // 如果 userId 为空，设置为 null
        form.value.cLeaveUser = null
      }
      
      // 新增时，如果没有设置状态，设置默认值（如果需要）
      if (form.value.id == null && !form.value.status) {
        // 可以根据业务需求设置默认状态，比如设置为"待审核"
        // form.value.status = "0" // 假设 0 是待审核状态
      }
      
      if (form.value.id != null) {
        updateLeave(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLeave(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除请假信息管理编号为"' + _ids + '"的数据项？').then(function() {
    return delLeave(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 设置已销假操作 */
function handleSetCompleted(row) {
  proxy.$modal.confirm(`是否确认将 ${row.cLeaveUser?.nickName || '该员工'} 的请假申请设置为已销假状态？`).then(function() {
    // 创建更新数据，将状态设置为已销假（假设状态值为3）
    const updateData = {
      ...row,
      status: 3 // 已销假状态
    }
    
    return updateLeave(updateData)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("已设置为销假状态")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('leave/leave/export', {
    ...queryParams.value
  }, `leave_${new Date().getTime()}.xlsx`)
}

/** 通过按钮操作 */
function handleApprove() {
  if (!form.value.id) {
    proxy.$modal.msgError("请选择要处理的请假申请")
    return
  }
  
  // 查找"通过"状态的值，支持多种命名方式
  const approveStatus = leave_status.value.find(dict => 
    dict.label === "通过" || 
    dict.label === "已通过" || 
    dict.label === "批准" ||
    dict.label === "已批准" ||
    dict.label.includes("通过") ||
    dict.value === "1" ||
    dict.value === "approved"
  )
  
  if (!approveStatus) {
    // 如果找不到，尝试使用值"1"作为默认值
    form.value.status = "1"
  } else {
    form.value.status = approveStatus.value
  }
  
  updateLeave(form.value).then(response => {
    proxy.$modal.msgSuccess("已通过")
    open.value = false
    getList()
  }).catch(error => {
    proxy.$modal.msgError("操作失败：" + (error.msg || "未知错误"))
  })
}

/** 拒绝按钮操作 */
function handleReject() {
  if (!form.value.id) {
    proxy.$modal.msgError("请选择要处理的请假申请")
    return
  }
  
  // 查找"拒绝"状态的值，支持多种命名方式
  const rejectStatus = leave_status.value.find(dict => 
    dict.label === "拒绝" || 
    dict.label === "已拒绝" || 
    dict.label === "驳回" ||
    dict.label === "已驳回" ||
    dict.label.includes("拒绝") ||
    dict.value === "2" ||
    dict.value === "rejected"
  )
  
  if (!rejectStatus) {
    // 如果找不到，尝试使用值"2"作为默认值
    form.value.status = "2"
  } else {
    form.value.status = rejectStatus.value
  }
  
  updateLeave(form.value).then(response => {
    proxy.$modal.msgSuccess("已拒绝")
    open.value = false
    getList()
  }).catch(error => {
    proxy.$modal.msgError("操作失败：" + (error.msg || "未知错误"))
  })
}

/** 获取用户昵称 */
function getUserNickName(userId) {
  if (!userId) return '未知用户'
  const user = userOptions.value.find(u => u.userId == userId)
  return user?.nickName || `用户${userId}`
}

/** 查询用户列表 */
function getUserList() {
  listUser({ pageNum: 1, pageSize: 1000 }).then(response => {
    // 过滤掉admin用户
    userOptions.value = (response.rows || []).filter(user => 
      user.userName !== 'admin' && user.nickName !== 'admin'
    )
  })
}

// 状态处理方法
function getStatusText(status) {
  if (!status && status !== 0) return '未知'
  
  // 优先使用字典数据
  if (leave_status.value && leave_status.value.length > 0) {
    const statusItem = leave_status.value.find(dict => String(dict.value) === String(status))
    if (statusItem) {
      return statusItem.label
    }
  }
  
  // 备用硬编码映射
  const statusMap = {
    '0': '已通过',
    '1': '已拒绝',
    '2': '待审核',
    '3': '已销假'
  }
  return statusMap[String(status)] || '未知'
}

function getStatusClass(status) {
  if (!status && status !== 0) return 'status-info'
  
  // 优先使用字典数据来判断样式
  if (leave_status.value && leave_status.value.length > 0) {
    const statusItem = leave_status.value.find(dict => String(dict.value) === String(status))
    if (statusItem) {
      const label = statusItem.label.toLowerCase()
      if (label.includes('通过') || label.includes('批准') || label.includes('approved')) {
        return 'status-success'
      } else if (label.includes('拒绝') || label.includes('驳回') || label.includes('rejected')) {
        return 'status-danger'
      } else if (label.includes('销假') || label.includes('completed')) {
        return 'status-info'
      } else if (label.includes('待') || label.includes('pending')) {
        return 'status-warning'
      }
    }
  }
  
  // 备用硬编码映射
  const classMap = {
    '0': 'status-success',
    '1': 'status-danger', 
    '2': 'status-warning',
    '3': 'status-info'
  }
  return classMap[String(status)] || 'status-info'
}

// 请假类型处理方法
function getLeaveTypeText(type) {
  if (!type && type !== 0) return '其他'
  
  // 优先使用字典数据
  if (leave_type.value && leave_type.value.length > 0) {
    const typeItem = leave_type.value.find(dict => String(dict.value) === String(type))
    if (typeItem) {
      return typeItem.label
    }
  }
  
  // 备用硬编码映射
  const typeMap = {
    '0': '事假',
    '1': '病假',
    '2': '年假',
    '3': '调休'
  }
  return typeMap[String(type)] || '其他'
}

function getLeaveTypeClass(type) {
  // 所有类型都使用统一样式
  return 'type-unified'
}

getList()
getUserList()
</script>

<style scoped lang="scss">
@import '@/assets/styles/system-pages.scss';

// Leave specific styles
.leave-id {
  font-weight: 600;
  color: var(--accent-primary);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  
  .user-icon {
    font-size: 16px;
    color: var(--accent-primary);
  }
  
  .user-name {
    font-weight: 500;
    color: var(--text-primary);
  }
}

.time-info {
  display: flex;
  align-items: center;
  gap: 6px;
  
  .time-icon {
    font-size: 14px;
    color: var(--text-secondary);
  }
}

.leave-type-tag {
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 8px;
  font-weight: 500;
  background: rgba(196, 149, 108, 0.1);
  color: var(--accent-primary);
  border: 1px solid rgba(196, 149, 108, 0.3);
  display: inline-block;
}

.leave-type-display {
  display: flex;
  align-items: center;
}

.reason-text {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
}

// 玻璃拟态底部按钮样式
.glass-dialog-footer {
  display: flex;
  gap: 12px;
  padding: 20px 28px;
  background: transparent;
  justify-content: center;
}

.glass-footer-btn {
  flex: 1;
  max-width: 120px;
  height: 44px;
  border: none;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  
  &.glass-footer-btn--success {
    background: rgba(103, 194, 58, 0.15);
    color: #67c23a;
    border: 1px solid rgba(103, 194, 58, 0.3);
    
    &:hover {
      background: rgba(103, 194, 58, 0.25);
      border-color: rgba(103, 194, 58, 0.5);
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(103, 194, 58, 0.2);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
  
  &.glass-footer-btn--danger {
    background: rgba(245, 108, 108, 0.15);
    color: #f56c6c;
    border: 1px solid rgba(245, 108, 108, 0.3);
    
    &:hover {
      background: rgba(245, 108, 108, 0.25);
      border-color: rgba(245, 108, 108, 0.5);
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(245, 108, 108, 0.2);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
  
  svg {
    font-size: 16px;
  }
}

.glass-dialog-footer-single {
  display: flex;
  justify-content: center;
  padding: 20px 28px;
  background: transparent;
}

.glass-status-display {
  display: flex;
  align-items: center;
  justify-content: center;
}

.glass-status-badge {
  padding: 12px 24px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  border: 1px solid;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  
  &.status-approved {
    background: rgba(103, 194, 58, 0.15);
    color: #67c23a;
    border-color: rgba(103, 194, 58, 0.3);
  }
  
  &.status-rejected {
    background: rgba(245, 108, 108, 0.15);
    color: #f56c6c;
    border-color: rgba(245, 108, 108, 0.3);
  }
}

.status-display {
  display: flex;
  align-items: center;
  
  .glass-status-tag {
    font-size: 14px;
    padding: 6px 12px;
    border-radius: 8px;
    font-weight: 500;
    border: 1px solid;
    
    &.status-success {
      background: rgba(103, 194, 58, 0.1);
      color: #67c23a;
      border-color: rgba(103, 194, 58, 0.3);
    }
    
    &.status-warning {
      background: rgba(230, 162, 60, 0.1);
      color: #e6a23c;
      border-color: rgba(230, 162, 60, 0.3);
    }
    
    &.status-danger {
      background: rgba(245, 108, 108, 0.1);
      color: #f56c6c;
      border-color: rgba(245, 108, 108, 0.3);
    }
    
    &.status-info {
      background: rgba(144, 147, 153, 0.1);
      color: #909399;
      border-color: rgba(144, 147, 153, 0.3);
    }
  }
}

// 居中显示内容
.centered-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
</style>
